package com.dzt.androidframework.json.car;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.dzt.androidframework.R;
import com.dzt.androidframework.activity.ActivityTitle;
import com.dzt.androidframework.adapter.AbsListAdapter;
import com.dzt.androidframework.photo.PhotoViewActivity;
import com.mob.mobapi.API;
import com.mob.mobapi.APICallback;
import com.mob.mobapi.MobAPI;
import com.mob.mobapi.apis.Car;
import com.squareup.picasso.Picasso;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CarSeriesDetailsActivity extends ActivityTitle implements APICallback {

	@ViewInject(R.id.lv_series)
	private ListView lvSeries;
	private String title;
	private String carImage;
	private MyAdapter adapter;
	private List<Map<String, Object>> datas = new ArrayList<>();

	@Override
	protected int getLayoutId() {
		return R.layout.activity_car_series_details;
	}

	@Override
	protected void initParams() {
		title = getIntent().getStringExtra("name");
		setTitleView(title);
		adapter = new MyAdapter(this, datas);
		lvSeries.setAdapter(adapter);
		String cid = getIntent().getExtras().getString("cid");
		//查询车型详细信息 cid和name必须填一个，默认使用cid
		Car api = (Car) MobAPI.getAPI(Car.NAME);
		api.querySeries(cid, this);
		showWaitDialog("加载中...", false, null);
	}

	@Override
	protected void initData(Bundle savedInstanceState) {

	}

	@Override
	protected String[] initPermissions() {
		return new String[0];
	}

	@Override
	public void onSuccess(API api, int action, Map<String, Object> result) {
		datas.clear();
		dismissDialog();
		try {
			List<Map<String, Object>> resList = (List<Map<String, Object>>) result.get("result");
			if (resList != null && resList.size() > 0) {
				Map<String, Object> res = resList.get(0);
				if (res != null && !res.isEmpty()) {
					carImage = (String) res.get("carImage");
					Map<String, Object> item;
					List<Map<String, Object>> info = (List<Map<String, Object>>) res.get("baseInfo");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_base_info));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("airConfig");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_air_config));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("carbody");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_car_body));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("chassis");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_chassis));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("controlConfig");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_control_config));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("engine");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_engine));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("exterConfig");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_exter_config));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("glassConfig");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_glass_config));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("interConfig");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_inter_config));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("lightConfig");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_light_config));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("mediaConfig");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_media_config));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("safetyDevice");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_safety_device));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("seatConfig");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_seat_config));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("techConfig");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_tech_config));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("transmission");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_transmission));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("wheelInfo");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_wheel_info));
						item.put("info", getInfo(info));
						datas.add(item);
					}

					info = (List<Map<String, Object>>) res.get("motorList");
					if (info != null && !info.isEmpty()) {
						item = new HashMap<>();
						item.put("name", getString(R.string.car_api_item_motor_list));
						item.put("info", getInfo(info));
						datas.add(item);
					}
				}
				ImageView headView = new ImageView(getBaseContext());
				if(carImage != null){
					Picasso.with(this)
							.load(carImage)
							.into(headView);
					lvSeries.addHeaderView(headView);
					headView.setOnClickListener(new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							Intent intent = new Intent();
							intent.putExtra("url", carImage);
							intent.putExtra("name", title);
							startActivity(PhotoViewActivity.class, intent);
						}
					});
				}
				adapter.notifyDataSetChanged();
			}
		} catch (Throwable t) {
			throw new RuntimeException(t);
		}
	}

	private String getInfo(List<Map<String, Object>> info) {
		StringBuffer sb = new StringBuffer();
		for (Map<String, Object> tmp : info) {
			sb.append(tmp.get("name"));
			sb.append(": ");
			sb.append(tmp.get("value"));
			sb.append("\n");
		}
		return sb.toString();
	}

	@Override
	public void onError(API api, int i, Throwable throwable) {
		dismissDialog();
	}

	private class MyAdapter extends AbsListAdapter<Map<String, Object>> {

		public MyAdapter(Context context, List<Map<String, Object>> datas) {
			super(context, datas);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = layoutInflater.inflate(R.layout.item_car_series_details, parent, false);
				x.view().inject(holder, convertView);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			Map<String, Object> item = (Map<String, Object>) getItem(position);
			holder.tvName.setText(String.valueOf(item.get("name")));
			holder.tvInfo.setText(String.valueOf(item.get("info")));
			return convertView;
		}
	}

	class ViewHolder {
		@ViewInject(R.id.tvName)
		TextView tvName;
		@ViewInject(R.id.tvInfo)
		TextView tvInfo;
	}
}
